package net.ant.watercalc.web.service.impl;

import net.ant.watercalc.web.pojo.bo.*;
import net.ant.watercalc.web.service.CalcService;
import net.ant.watercalc.web.utils.KspUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;

@Service
public class CalcServiceImpl implements CalcService {

    @Override
    public String getCationsTotal(Cations cations) {
        return null;
    }

    @Override
    public String getAnionsTotal(Anions anions) {
        return null;
    }

    /**
     * 获取进水量 m3/h
     *
     * @param ff       进水量
     * @param feedType 类型
     * @return
     */
    @Override
    public String getFeedFlow(String ff, int feedType) {
        BigDecimal bd = new BigDecimal(ff);
        BigDecimal factor = new BigDecimal(1);
        switch (feedType) {
            case 1:
                factor = new BigDecimal(0.003785).multiply(new BigDecimal(60));
                break;
            case 2:
                factor = new BigDecimal(0.003785).divide(new BigDecimal(24), 6, BigDecimal.ROUND_HALF_UP);
                break;
            case 3:
                factor = new BigDecimal(0.004546).multiply(new BigDecimal(60));
                break;
            case 4:
                factor = new BigDecimal(0.004546).divide(new BigDecimal(24), 6, BigDecimal.ROUND_HALF_UP);
                break;
            case 5:
                factor = new BigDecimal(1 / 24);
                break;
            default:
                break;
        }
        return bd.multiply(factor).setScale(2, BigDecimal.ROUND_HALF_DOWN).toPlainString();
    }

    /**
     * 获取修正ph的H+ 和OH-
     *
     * @param tempType 0: C 1:F
     * @param ph
     * @param rawTemp
     * @return
     */
    @Override
    public WaterQ getWaterQ(int tempType, String ph, String rawTemp) {
        WaterQ waterQ = new WaterQ();

        double temprature = tempType == 1 ? (Float.parseFloat(rawTemp) - 32) / 1.8 : Float.parseFloat(rawTemp);
        waterQ.setTemprature(temprature);
        waterQ.setVal_H(Math.pow(10, -(new BigDecimal(ph).floatValue())));
        waterQ.setVal_OH(Math.pow(10, -14 + new BigDecimal(ph).floatValue()));
        return waterQ;
    }

    /**
     * 修正的碱度计算
     *
     * @param oh
     * @param waterQ
     * @return
     */
    @Override
    public OHResult getOH_Adjusted(OHResult oh, WaterQ waterQ) {
        OHResult ohResult = new OHResult();
        ohResult.setCt(oh.getCt());
        ohResult.setR((waterQ.getVal_H() - waterQ.getVal_OH()) * 1000);
        ohResult.setTemp(waterQ.getTemprature() + 273.15);
        ohResult.setPk1(3447 / ohResult.getTemp() - 15.08 + (0.0331 * ohResult.getTemp()));
        ohResult.setPk2(2929 / ohResult.getTemp() - 6.65 + (0.024 * ohResult.getTemp()));
        ohResult.setK1(Math.pow(0.1, ohResult.getPk1()));
        ohResult.setK2(Math.pow(0.1, ohResult.getPk2()));
        ohResult.setA0(Math.pow((1 + ohResult.getK1() / waterQ.getVal_H() + ohResult.getK1() * ohResult.getK2() / Math.pow(waterQ.getVal_H(), 2)), -1));
        ohResult.setA1(Math.pow(waterQ.getVal_H() / ohResult.getK1() + 1 + ohResult.getK2() / waterQ.getVal_H(), -1));
        ohResult.setA2(Math.pow(Math.pow(waterQ.getVal_H(), 2) / (ohResult.getK1() * ohResult.getK2()) + waterQ.getVal_H() / ohResult.getK2() + 1, -1));
        ohResult.setTotal(ohResult.getCt() * (ohResult.getA1() + 2 * ohResult.getA2()) + (waterQ.getVal_OH() - waterQ.getVal_H()) * 1000);
        ohResult.setCo2(ohResult.getCt() * ohResult.getA0() * 44);
        ohResult.setCo3(ohResult.getCt() * ohResult.getA2() * 60 * 2);
        ohResult.setHco3(ohResult.getCt() * ohResult.getA1() * 61);

        return ohResult;
    }

    /**
     * 获取原水碱度
     *
     * @param waterQ
     * @param anions
     * @param cations
     * @return
     */
    private OHResult getOH_Raw(WaterQ waterQ, Anions anions, Cations cations) {
        OHResult ohResult = new OHResult();
        double hco3 = anions.getHco3()[3];
        double na = cations.getNa()[0];
        double cl = anions.getCl()[0];
        double so4 = anions.getSo4()[0];

        ohResult.setTemp(waterQ.getTemprature() + 273.15);
        ohResult.setPk1(3447 / ohResult.getTemp() - 15.08 + 0.0331 * ohResult.getTemp());
        ohResult.setPk2(2929 / ohResult.getTemp() - 6.65 + 0.024 * ohResult.getTemp());
        ohResult.setK1(Math.pow(0.1, ohResult.getPk1()));
        ohResult.setK2(Math.pow(0.1, ohResult.getPk2()));
        ohResult.setA0(Math.pow(1 + ohResult.getK1() / waterQ.getVal_H() + ohResult.getK1() * ohResult.getK2() / Math.pow(waterQ.getVal_H(), 2), -1));
        ohResult.setA1(Math.pow(waterQ.getVal_H() / ohResult.getK1() + 1 + ohResult.getK2() / waterQ.getVal_H(), -1));
        ohResult.setA2(Math.pow(Math.pow(waterQ.getVal_H(), 2) / (ohResult.getK1() * ohResult.getK2()) + waterQ.getVal_H() / ohResult.getK2() + 1, -1));
        ohResult.setR((waterQ.getVal_H() - waterQ.getVal_OH()) * 1000);
        ohResult.setCt(hco3 / ohResult.getA1());
        ohResult.setTotal(ohResult.getCt() * ohResult.getA1() + 2 * ohResult.getA2() + (waterQ.getVal_OH() - waterQ.getVal_H()) * 1000);
        ohResult.setCo2(ohResult.getCt() * ohResult.getA0() * 44);
        ohResult.setCo3(ohResult.getCt() * ohResult.getA2() * 60 * 2);
        ohResult.setHco3(ohResult.getCt() * ohResult.getA1() * 61);
        return ohResult;
    }

    /**
     * 修正 na,cl,na,so4
     *
     * @param eType
     * @param data
     * @return
     */
    private AdjustCAAN getAdjustVal(String eType, String[] data) {

        double[] hcoArr = new double[4];
        double[] naArr = new double[4];
        double[] clArr = new double[4];
        double[] so4Arr = new double[4];

        double temp = new BigDecimal(data[0]).multiply(new BigDecimal(data[1])).multiply(new BigDecimal(61)).doubleValue();
        hcoArr[0] = new BigDecimal(temp).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
        hcoArr[1] = new BigDecimal(temp).multiply(new BigDecimal(0.8196063)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
        hcoArr[2] = new BigDecimal(temp).multiply(new BigDecimal(0.01639213)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
        hcoArr[3] = new BigDecimal(temp).multiply(new BigDecimal(0.01639213)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();

        switch (eType.toUpperCase()) {
            case "C":
                temp = new BigDecimal(data[2]).add((new BigDecimal(data[3]).multiply(new BigDecimal(0.575)))).doubleValue();
                naArr[0] = temp;
                naArr[1] = new BigDecimal(temp).multiply(new BigDecimal(2.174859)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                naArr[2] = new BigDecimal(temp).multiply(new BigDecimal(0.04349717)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                naArr[3] = new BigDecimal(temp).multiply(new BigDecimal(0.04349717)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                break;
            case "A":
                temp = new BigDecimal(data[2]).add((new BigDecimal(data[3]).multiply(new BigDecimal(0.973)))).doubleValue();
                clArr[0] = temp;
                clArr[1] = new BigDecimal(temp).multiply(new BigDecimal(1.410318)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                clArr[2] = new BigDecimal(temp).multiply(new BigDecimal(0.02820636)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                clArr[3] = new BigDecimal(temp).multiply(new BigDecimal(0.02820636)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                break;
            case "B":
                temp = new BigDecimal(data[2]).add((new BigDecimal(data[3])).multiply(new BigDecimal(0.98))).doubleValue();
                so4Arr[0] = temp;
                so4Arr[1] = new BigDecimal(temp).multiply(new BigDecimal(1.041667)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                so4Arr[2] = new BigDecimal(temp).multiply(new BigDecimal(0.02083333)).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                so4Arr[3] = new BigDecimal(temp).multiply(new BigDecimal(0.02083333)).divide(new BigDecimal(2), 8, BigDecimal.ROUND_HALF_UP).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
                break;
            default:
                break;
        }
        AdjustCAAN adjustCAAN = new AdjustCAAN();
        adjustCAAN.setHco3(hcoArr);
        adjustCAAN.setNa(naArr);
        adjustCAAN.setCl(clArr);
        adjustCAAN.setSo4(so4Arr);
        return adjustCAAN;
    }

    /**
     * 检测平衡
     *
     * @param rawCations
     * @param rawAnions
     * @param adjustCA_AN
     * @return
     */
    private BalanceResult checkBalance(Cations rawCations, Anions rawAnions, AdjustCAAN adjustCA_AN) {
        String ct_meq = new BigDecimal(rawCations.getNh4()[2]).add(new BigDecimal(rawCations.getK()[2])).add(new BigDecimal(adjustCA_AN.getNa()[2])).add(new BigDecimal(rawCations.getMg()[2])).add(new BigDecimal(rawCations.getCa()[2])).add(new BigDecimal(rawCations.getSr()[2])).add(new BigDecimal(rawCations.getBa()[2])).toPlainString();
        String at_meq = new BigDecimal(adjustCA_AN.getCo3()[2]).add(new BigDecimal(adjustCA_AN.getHco3()[2])).add(new BigDecimal(adjustCA_AN.getCl()[2])).add(new BigDecimal(rawAnions.getF()[2])).add(new BigDecimal(adjustCA_AN.getSo4()[2])).add(new BigDecimal(rawAnions.getNo3()[2])).toPlainString();

        BalanceResult balanceResult = new BalanceResult();
        balanceResult.setEqualed(ct_meq.equals(at_meq));
        balanceResult.setAdjustCAAN(adjustCA_AN);
        balanceResult.setCtmeq(ct_meq);
        balanceResult.setAtmeq(at_meq);
        return balanceResult;
    }

    /**
     * 获取Dosage计算值
     *
     * @param type     0: not need 1: naoh 2: h2so4
     * @param adjustOh
     * @param rawOh
     * @param chemType
     * @return
     */
    private String getDosage(int type, OHResult adjustOh, OHResult rawOh, String chemType) {

        switch (type) {
            case 1:
                return new BigDecimal(adjustOh.getTotal()).subtract(new BigDecimal(rawOh.getTotal())).multiply(new BigDecimal(40)).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
            case 2:
                if ("HCL".equalsIgnoreCase(chemType)) {
                    return new BigDecimal(rawOh.getTotal()).subtract(new BigDecimal(adjustOh.getTotal())).multiply(new BigDecimal(36.5)).toPlainString();
                } else if ("H2SO4".equalsIgnoreCase(chemType)) {
                    return new BigDecimal(rawOh.getTotal()).subtract(new BigDecimal(adjustOh.getTotal())).multiply(new BigDecimal(98)).divide(new BigDecimal(2), 8, BigDecimal.ROUND_HALF_UP).toPlainString();
                }
                break;
            default:
                break;
        }
        return "0";
    }

    /**
     * 原水计算结果列表
     *
     * @param rawAnions
     * @param rawCations
     * @param rawOh
     * @param temp
     * @param ph
     * @return
     */
    @Override
    public ListResult getListResult(Anions rawAnions, Cations rawCations, OHResult rawOh, float temp, float ph) {
        ListResult listResult = new ListResult();
        rawAnions.getCo3()[0] = rawOh.getCo3();
        rawAnions.getCo3()[1] = rawOh.getCo3() * 1.6667;
        rawAnions.getCo3()[2] = rawOh.getCo3() * 0.033333333;
        rawAnions.getCo3()[3] = rawOh.getCo3() * 0.033333333 / 2;

        listResult.setCo2(rawOh.getCo2());
        listResult.setCo3(rawOh.getCo3());
        listResult.setHco3(rawOh.getHco3());

        listResult.setPh(ph);
        float phc = new BigDecimal(rawOh.getPk1()).add(new BigDecimal(Math.log10(new BigDecimal(rawAnions.getHco3()[2]).divide((new BigDecimal(rawOh.getCo2()).divide(new BigDecimal(44), 8, BigDecimal.ROUND_HALF_UP)), 8, BigDecimal.ROUND_HALF_UP).floatValue()))).floatValue();
        float pca = new BigDecimal(-0.4343).multiply(new BigDecimal(Math.log(new BigDecimal(rawCations.getCa()[1]).floatValue()))).add(new BigDecimal(5)).floatValue();
        double palk = new BigDecimal(-0.45).multiply(new BigDecimal(Math.log(new BigDecimal(rawAnions.getHco3()[1]).floatValue()))).add(new BigDecimal(4.8)).floatValue();

        double temp_1 = rawCations.getCa()[3] + rawCations.getMg()[3] + rawAnions.getSo4()[3] + rawAnions.getCo3()[3] + rawCations.getSr()[3] + rawCations.getBa()[3];
        double temp_2 = rawCations.getNa()[3] + rawAnions.getCl()[3] + rawAnions.getHco3()[3] + rawCations.getNh4()[3] + rawCations.getK()[3] + rawAnions.getF()[3] + rawAnions.getNo3()[3];
        double ionic = 0.5 * (4 * temp_1 * 0.001 + 1 * temp_2 * 0.001);
        listResult.setIonic(ionic);

        float cation_mg = new BigDecimal(rawCations.getNh4()[0]).add(new BigDecimal(rawCations.getK()[0])).add(new BigDecimal(rawCations.getNa()[0])).add(new BigDecimal(rawCations.getMg()[0])).add(new BigDecimal(rawCations.getCa()[0])).add(new BigDecimal(rawCations.getSr()[0])).add(new BigDecimal(rawCations.getBa()[0])).add(new BigDecimal(rawCations.getFe())).add(new BigDecimal(rawCations.getMn())).add(new BigDecimal(rawCations.getAl())).floatValue();
        float anions_mg = new BigDecimal(rawAnions.getCo3()[0]).add(new BigDecimal(rawAnions.getHco3()[0])).add(new BigDecimal(rawAnions.getNo3()[0])).add(new BigDecimal(rawAnions.getCl()[0])).add(new BigDecimal(rawAnions.getF()[0])).add(new BigDecimal(rawAnions.getSo4()[0])).add(new BigDecimal(rawAnions.getSio2())).floatValue();
        listResult.setTds(new BigDecimal(cation_mg).add(new BigDecimal(anions_mg)).floatValue());

        float c = new BigDecimal(0.03742).multiply(new BigDecimal(Math.log(new BigDecimal(listResult.getTds()).floatValue()))).subtract((new BigDecimal(0.0209).multiply(new BigDecimal(temp)))).add(new BigDecimal(2.5)).floatValue();
        float phs = new BigDecimal(pca).add(new BigDecimal(palk)).add(new BigDecimal(c)).floatValue();

        listResult.setLangelier(new BigDecimal(phc).subtract(new BigDecimal(phs)).floatValue());

        double k = 3.78342 + 0.16781 * Math.log(listResult.getIonic()) - 0.26411 * Math.pow(Math.log(listResult.getIonic()), 2) - 0.1029 * Math.pow(Math.log(listResult.getIonic()), 3) - 0.01124 * Math.pow(Math.log(listResult.getIonic()), 4) - 0.01221 * temp - 0.0001316 * Math.pow(temp, 2);
        float phv = new BigDecimal(pca).add(new BigDecimal(palk)).add(new BigDecimal(k)).floatValue();

        double tempF = listResult.getIonic();
        double tempIonic = listResult.getIonic();
        if (tempIonic < 0.012) {
            tempF = 0.012F;
        } else if (tempIonic > 2) {
            tempF = 2F;
        }

        String temp_ksp = KspUtil.getValueByKspType(1, tempF);
        String ksp = String.valueOf(Float.parseFloat(temp_ksp) * 0.0001);
        listResult.setCaso4((rawCations.getCa()[3] * 0.001 * rawAnions.getSo4()[3] * 0.001) / Float.parseFloat(ksp) * 100);

        if (tempIonic < 0.01) {
            tempF = 0.01F;
        } else if (tempIonic > 1.4) {
            tempF = 1.4F;
        } else {
            tempF = tempIonic;
        }
        ksp = new BigDecimal(KspUtil.getValueByKspType(2, tempF)).multiply(new BigDecimal(0.000000001)).toPlainString();
        listResult.setBaso4(new BigDecimal(rawCations.getBa()[3]).multiply(new BigDecimal(0.001)).multiply(new BigDecimal(rawAnions.getSo4()[3])).multiply(new BigDecimal(0.001)).divide(new BigDecimal(ksp), 8, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).floatValue());

        if (tempIonic < 0.01) {
            tempF = 0.01F;
        } else if (tempIonic > 2) {
            tempF = 2.0F;
        } else {
            tempF = tempIonic;
        }
        ksp = new BigDecimal(KspUtil.getValueByKspType(3, tempF)).multiply(new BigDecimal(0.000001)).toPlainString();
        listResult.setSrso4(new BigDecimal(rawCations.getSr()[3]).multiply(new BigDecimal(0.001)).multiply(new BigDecimal(rawAnions.getSo4()[3])).multiply(new BigDecimal(0.001)).divide(new BigDecimal(ksp), 8, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).floatValue());

        ksp = new BigDecimal(5.2).multiply(new BigDecimal(Math.pow(10, -11))).toPlainString();
        listResult.setCaf2(new BigDecimal(rawCations.getCa()[3]).multiply(new BigDecimal(0.001)).multiply(new BigDecimal(Math.pow(new BigDecimal(rawAnions.getF()[3]).multiply(new BigDecimal(0.001)).floatValue(), 2))).divide(new BigDecimal(ksp), 8, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).floatValue());

        double temp_c = temp;
        if (temp > 38) {
            temp_c = 38F;
        }

        tempF = ph;
        if (ph > 10.04) {
            tempF = 10.04F;
        } else if (ph < 4) {
            tempF = 4.0F;
        }
        ksp = new BigDecimal(KspUtil.getValueByKspType(5, temp_c)).multiply(new BigDecimal(KspUtil.getValueByKspType(4, tempF))).toPlainString();
        listResult.setSio2(new BigDecimal(rawAnions.getSio2()).divide(new BigDecimal(ksp), 8, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).floatValue());
        listResult.setStiff(new BigDecimal(phc).subtract(new BigDecimal(phv)).floatValue());

        return listResult;
    }

    /**
     * 平衡值计算结果
     *
     * @param adjustCAAN
     * @param rawCations
     * @param rawAnions
     * @param adjustOHVal
     * @param rawOHVal
     * @param ph
     * @param needAdjust
     * @param temperature
     * @return
     */
    @Override
    public ListResult getAdjustedListResult(AdjustCAAN adjustCAAN, Cations rawCations, Anions rawAnions, OHResult adjustOHVal, OHResult rawOHVal, float ph, boolean needAdjust, float temperature) {
        OHResult tempSub;
        ListResult adjustResult = new ListResult();
        adjustResult.setPh(ph);
        if (needAdjust) {
            tempSub = adjustOHVal;
            adjustResult.setHco3(tempSub.getHco3());
        } else {
            tempSub = rawOHVal;
            adjustResult.setHco3(rawAnions.getHco3()[0]);
        }

        adjustResult.setCo2(tempSub.getCo2());
        adjustResult.setCo3(tempSub.getCo3());

        double phc = tempSub.getPk1() + Math.log10(adjustCAAN.getHco3()[2] / (tempSub.getCo2() / 44));
        double pca = -0.4343 * Math.log(rawCations.getCa()[1]) + 5;
        double palk = -0.45 * Math.log(adjustCAAN.getHco3()[1]) + 4.8;

        double ionic = 0.5 * (4 * (rawCations.getCa()[3] + rawCations.getMg()[3] + adjustCAAN.getSo4()[3] + adjustCAAN.getCo3()[3] + rawCations.getSr()[3] + rawCations.getBa()[3]) * 0.001 + 1 * (adjustCAAN.getNa()[3] + adjustCAAN.getCl()[3] + adjustCAAN.getHco3()[3] + rawCations.getNh4()[3] + rawCations.getK()[3] + rawAnions.getF()[3] + rawAnions.getNo3()[3]) * 0.001);
        adjustResult.setIonic(ionic);

        double temp_cation = rawCations.getNh4()[0] + rawCations.getK()[0] + adjustCAAN.getNa()[0] + rawCations.getMg()[0] + rawCations.getCa()[0] + rawCations.getSr()[0] + rawCations.getBa()[0] + rawCations.getFe() + rawCations.getMn() + rawCations.getAl();
        double temp_anions = adjustCAAN.getCo3()[0] + adjustCAAN.getHco3()[0] + rawAnions.getNo3()[0] + adjustCAAN.getCl()[0] + rawAnions.getF()[0] + adjustCAAN.getSo4()[0] + rawAnions.getSio2();
        adjustResult.setTds(temp_anions + temp_cation);

        double c = 0.03742 * Math.log(temp_cation + temp_anions) - 0.0209 * temperature + 2.5;
        double phs = pca + palk + c;

        double k = 3.78342 + 0.16781 * Math.log(adjustResult.getIonic()) - 0.26411 * Math.pow(Math.log(adjustResult.getIonic()), 2) - 0.1029 * Math.pow(Math.log(adjustResult.getIonic()), 3) - 0.01124 * Math.pow(Math.log(adjustResult.getIonic()), 4) - 0.01221 * temperature - 0.0001316 * Math.pow(temperature, 2);
        double phv = pca + palk + k;

        adjustResult.setLangelier(phc - phs);
        adjustResult.setStiff(phc - phv);

        double tempF;
        double ionic_temp = adjustResult.getIonic();
        if (ionic < 0.012F) {
            tempF = 0.012F;
        } else if (ionic > 2.0F) {
            tempF = 2.0F;
        } else {
            tempF = ionic_temp;
        }

        String ksp = "" + (Float.parseFloat(KspUtil.getValueByKspType(1, tempF)) * 0.0001);
        adjustResult.setCaso4(rawCations.getCa()[3] * 0.001 * adjustCAAN.getSo4()[3] * 0.001 / Float.parseFloat(ksp) * 100);

        if (ionic < 0.01F) {
            tempF = 0.01F;
        } else if (ionic > 1.4F) {
            tempF = 1.4F;
        } else {
            tempF = ionic_temp;
        }

        ksp = "" + (Float.parseFloat(KspUtil.getValueByKspType(2, tempF)) * 0.000000001);
        adjustResult.setBaso4(rawCations.getBa()[3] * 0.001 * adjustCAAN.getSo4()[3] * 0.001 / Float.parseFloat(ksp) * 100);

        if (ionic < 0.01F) {
            tempF = 0.01F;
        } else if (ionic > 2.0F) {
            tempF = 2.0F;
        } else {
            tempF = ionic_temp;
        }
        ksp = "" + (Float.parseFloat(KspUtil.getValueByKspType(3, tempF)) * 0.000001);
        adjustResult.setSrso4(rawCations.getSr()[3] * 0.001 * adjustCAAN.getSo4()[3] * 0.001 / Float.parseFloat(ksp) * 100);

        ksp = "" + (5.2 * Math.pow(10, -11));
        adjustResult.setCaf2(rawCations.getCa()[3] * 0.001 * Math.pow(rawAnions.getF()[3] * 0.001, 2) / Float.parseFloat(ksp) * 100);


        Float tempC = temperature;
        if (temperature > 38) {
            tempC = 38F;
        }
        if (adjustResult.getPh() > 10.04) {
            tempF = 10.04F;
        } else if (adjustResult.getPh() < 4) {
            tempF = 4F;
        } else {
            tempF = adjustResult.getPh();
        }
        ksp = "" + (Float.parseFloat(KspUtil.getValueByKspType(5, tempC)) * Float.parseFloat(KspUtil.getValueByKspType(4, tempF)));
        adjustResult.setSio2(rawAnions.getSio2() / Float.parseFloat(ksp) * 100);
        return adjustResult;
    }

    /**
     * //浓水计算结果
     *
     * @param adjustCAAN
     * @param rawAnions
     * @param rawCations
     * @param chloride
     * @param sysRecv
     * @param temperature
     * @param adjustResult
     * @return
     */
    @Override
    public ListResult getListResultConcentrade(AdjustCAAN adjustCAAN, Anions rawAnions, Cations rawCations, float chloride, float sysRecv, float temperature, ListResult adjustResult, CoElement coElement,Cations conCation,Anions conAnions) {
        OHResult tempSub = new OHResult();
        ListResult concentradeResult = new ListResult();

        tempSub.setTemp(temperature + 273.15);
        tempSub.setPk1(3447 / tempSub.getTemp() - 15.08 + 0.0331 * tempSub.getTemp());
        double conrate = (chloride * 0.01) / (1 - sysRecv * 0.01);
        double conco2 = adjustResult.getCo2();

        double[] valArr = new double[4];

        valArr[0] = rawCations.getNh4()[0] * conrate;
        valArr[1] = valArr[0] * 2.7721834;
        valArr[2] = valArr[0] * 0.05543668;
        valArr[3] = valArr[2];
        conCation.setNh4(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawCations.getK()[0] * conrate;
        valArr[1] = valArr[0] * 1.278707;
        valArr[2] = valArr[0] * 0.02557414;
        valArr[3] = valArr[2];
        conCation.setK(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = adjustCAAN.getNa()[0] * conrate;
        valArr[1] = valArr[0] * 2.174859;
        valArr[2] = valArr[0] * 0.04349717;
        valArr[3] = valArr[2];
        conCation.setNa(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawCations.getMg()[0] * conrate;
        valArr[1] = valArr[0] * 4.113195;
        valArr[2] = valArr[0] * 0.08226391;
        valArr[3] = valArr[2] / 2;
        conCation.setMg(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawCations.getCa()[0] * conrate;
        valArr[1] = valArr[0] * 2.49501;
        valArr[2] = valArr[0] * 0.0499002;
        valArr[3] = valArr[2] / 2;
        conCation.setCa(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawCations.getSr()[0] * conrate;
        valArr[1] = valArr[0] * 1.141292;
        valArr[2] = valArr[0] * 0.02282584;
        valArr[3] = valArr[2] / 2;
        conCation.setSr(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawCations.getBa()[0] * conrate;
        valArr[1] = valArr[0] * 0.72812;
        valArr[2] = valArr[0] * 0.0145624;
        valArr[3] = valArr[2] / 2;
        conCation.setBa(Arrays.copyOf(valArr, valArr.length));

        conCation.setFe(rawCations.getFe() * conrate);
        conCation.setMn(rawCations.getMn() * conrate);
        conCation.setAl(rawCations.getAl() * conrate);

        valArr[0] = adjustResult.getHco3() * conrate;
        valArr[1] = valArr[0] * 0.8196063;
        valArr[2] = valArr[0] * 0.01639213;
        valArr[3] = valArr[2];
        conAnions.setHco3(Arrays.copyOf(valArr, valArr.length));

        double conph = tempSub.getPk1() + Math.log10(conAnions.getHco3()[2] / (conco2 / 44));
        concentradeResult.setPh(new BigDecimal(conph).setScale(2,BigDecimal.ROUND_HALF_UP).floatValue());

        tempSub.setK1(Math.pow(0.1, tempSub.getPk1()));
        tempSub.setPk2(2929 / tempSub.getTemp() - 6.65 + 0.024 * tempSub.getTemp());
        tempSub.setK2(Math.pow(0.1, tempSub.getPk2()));

        WaterQ conWaterQ = new WaterQ();
        conWaterQ.setVal_H(Math.pow(10, -conph));

        tempSub.setA2(Math.pow(Math.pow(conWaterQ.getVal_H(), 2) / (tempSub.getK1() * tempSub.getK2()) + conWaterQ.getVal_H() / tempSub.getK2() + 1, -1));
        tempSub.setA1(Math.pow(conWaterQ.getVal_H() / tempSub.getK1() + 1 + tempSub.getK2() / conWaterQ.getVal_H(), -1));
        tempSub.setCt(conAnions.getHco3()[2] / tempSub.getA1());
        //tempSub.setCo3(tempSub.getCt() * tempSub.getA2() * 60 * 2);
        tempSub.setCo3(adjustResult.getCo3()*conrate);
        tempSub.setA0(Math.pow(1 + tempSub.getK1() / conWaterQ.getVal_H() + tempSub.getK1() * tempSub.getK2() / Math.pow(conWaterQ.getVal_H(), 2), -1));

        concentradeResult.setCo3(tempSub.getCo3());
        concentradeResult.setHco3(tempSub.getCt() * tempSub.getA1() * 61);

        valArr[0] = tempSub.getCo3();
        valArr[1] = tempSub.getCo3() * 1.6667;
        valArr[2] = tempSub.getCo3() * 0.033333333;
        valArr[3] = valArr[2] / 2;
        conAnions.setCo3(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawAnions.getNo3()[0] * conrate;
        valArr[1] = valArr[0] * 0.8063879;
        valArr[2] = valArr[1] * 0.01612776;
        valArr[3] = valArr[2] / 2;
        conAnions.setNo3(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = adjustCAAN.getCl()[0] * conrate;
        valArr[1] = valArr[0] * 1.410318;
        valArr[2] = valArr[0] * 0.02820636;
        valArr[3] = valArr[2];
        conAnions.setCl(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = rawAnions.getF()[0] * conrate;
        valArr[1] = valArr[0] * 2.631801;
        valArr[2] = valArr[0] * 0.05263601;
        valArr[3] = valArr[2];
        conAnions.setF(Arrays.copyOf(valArr, valArr.length));

        valArr[0] = adjustCAAN.getSo4()[0] * conrate;
        valArr[1] = valArr[0] * 1.041667;
        valArr[2] = valArr[0] * 0.02083333;
        valArr[3] = valArr[2] / 2;
        conAnions.setSo4(Arrays.copyOf(valArr, valArr.length));

        conAnions.setSio2(rawAnions.getSio2() * conrate);

        double ionic = 0.5 * (4 * (conCation.getCa()[3] + conCation.getMg()[3] + conAnions.getSo4()[3] + conAnions.getCo3()[3] + conCation.getSr()[3] + conCation.getBa()[3]) * 0.001 + 1 * (conCation.getNa()[3] + conAnions.getCl()[3] + conAnions.getHco3()[3] + conCation.getNh4()[3] + conCation.getK()[3] + conAnions.getF()[3] + conAnions.getNo3()[3]) * 0.001);
        concentradeResult.setIonic(ionic);
        double cation_mg = conCation.getNh4()[0] + conCation.getK()[0] + conCation.getNa()[0] + conCation.getMg()[0] + conCation.getCa()[0] + conCation.getSr()[0] + conCation.getBa()[0] + conCation.getFe() + conCation.getMn() + conCation.getAl();
        double anions_mg = conAnions.getCo3()[0] + conAnions.getHco3()[0] + conAnions.getNo3()[0] + conAnions.getCl()[0] + conAnions.getF()[0] + conAnions.getSo4()[0] + conAnions.getSio2();

        concentradeResult.setTds(cation_mg + anions_mg);
        concentradeResult.setCo2(tempSub.getCt() * tempSub.getA0() * 44);

        double phc = tempSub.getPk1() + Math.log10(conAnions.getHco3()[2] / (concentradeResult.getCo2() / 44));
        double pca = -0.4343 * Math.log(conCation.getCa()[1]) + 5;
        double palk = -0.45 * Math.log(conAnions.getHco3()[1]) + 4.8;
        double c = 0.03742 * Math.log(cation_mg + anions_mg) - 0.0209 * temperature + 2.5;
        double phs = pca + palk + c;

        double k = 3.78342 + 0.16781 * Math.log(concentradeResult.getIonic()) - 0.26411 * Math.pow(Math.log(concentradeResult.getIonic()), 2) - 0.1029 * Math.pow(Math.log(concentradeResult.getIonic()), 3) - 0.01124 * Math.pow(Math.log(concentradeResult.getIonic()), 4) - 0.01221 * temperature - 0.0001316 * Math.pow(temperature, 2);
        double phv = pca + palk + k;

        concentradeResult.setLangelier(phc - phs);
        concentradeResult.setStiff(phc - phv);

        double tempF;
        double ionic_temp = concentradeResult.getIonic();

        if (ionic < 0.012) {
            tempF = 0.012F;
        } else if (ionic > 2) {
            tempF = 2F;
        } else {
            tempF = ionic_temp;
        }
        double ksp = (Float.parseFloat(KspUtil.getValueByKspType(1, tempF)) * 0.0001);
        concentradeResult.setCaso4(conCation.getCa()[3] * 0.001 * conAnions.getSo4()[3] * 0.001 / ksp * 100);

        if (ionic < 0.01) {
            tempF = 0.01F;
        } else if (ionic > 1.4) {
            tempF = 1.4F;
        } else {
            tempF = ionic_temp;
        }
        ksp = Float.parseFloat(KspUtil.getValueByKspType(2, tempF)) * 0.000000001;
        concentradeResult.setBaso4(conCation.getBa()[3] * 0.001 * conAnions.getSo4()[3] * 0.001 / ksp * 100);

        if (ionic < 0.01) {
            tempF = 0.01F;
        } else if (ionic > 2) {
            tempF = 2F;
        } else {
            tempF = ionic_temp;
        }
        ksp = Float.parseFloat(KspUtil.getValueByKspType(3, tempF)) * 0.000001;
        concentradeResult.setSrso4(conCation.getSr()[3] * 0.001 * conAnions.getSo4()[3] * 0.001 / ksp * 100);

        ksp = 5.2 * Math.pow(10, -11);
        concentradeResult.setCaf2(conCation.getCa()[3] * 0.001 * Math.pow(conAnions.getF()[3] * 0.001, 2) / ksp * 100);

        double tempC = temperature;
        if (temperature > 38) {
            tempC = 38;
        }

        if (conph > 10.04) {
            tempF = 10.04F;
        } else if (conph < 4) {
            tempF = 4F;
        } else {
            tempF = conph;
        }
        ksp = Float.parseFloat(KspUtil.getValueByKspType(5, tempC)) * Float.parseFloat(KspUtil.getValueByKspType(4, tempF));
        concentradeResult.setSio2(conAnions.getSio2() / ksp * 100);

        coElement.setMg(conCation.getMg()[0]);
        coElement.setSio2(conAnions.getSio2());
        coElement.setFe(new double[]{rawCations.getFe(),conCation.getFe()});
        coElement.setAl(new double[]{rawCations.getAl(),conCation.getAl()});
        coElement.setMn(new double[]{rawCations.getMn(),conCation.getMn()});

        return concentradeResult;
    }
}
